package com.atguigu.servlet;

import com.atguigu.bean.Book;
import com.atguigu.bean.Page;
import com.atguigu.service.BookService;
import com.atguigu.service.impl.BookServiceImpl;
import com.atguigu.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Date:2020/9/11
 * Author:ybc
 * Description:
 */
public class BookManagerServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();


    protected void getPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取请求参数
            String pageNoStr = request.getParameter("pageNo");
            //创建page对象
            Page<Book> pageInfo = new Page<>();
            pageInfo.setPageNostr(pageNoStr);
            //通过工具类WebUtils动态获取请求路径
        String path = WebUtils.getPath(request);
        pageInfo.setPath(path);

        //调用service处理业务逻辑，即完善page对象的过程
        Page<Book> page = bookService.getPage(pageInfo);
        //将page对象在request请求域中进行共享
        request.setAttribute("page",page);
            //转发到book_manager.jsp
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);

    }

    /**
     * 查询所有的图书信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getBookList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //调用service处理业务逻辑，实现功能
        List<Book> list = bookService.getBookList();
        //将所有的图书信息在request请求域中进行共享
        request.setAttribute("list", list);
        //通过转发跳转到book_manager.jsp
        request.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(request, response);
    }

    /**
     * 跳转到修改页面，同时根据id获取图书信息，并在修改页面中回显
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void toUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String id = request.getParameter("id");
        //调用service处理业务逻辑，通过id获取要修改的图书信息
        Book book = bookService.getBookByBookId(Integer.parseInt(id));
        //将要修改的图书信息在request请求域中共享
        request.setAttribute("book", book);
        //通过转发跳转到修改页面
        request.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(request, response);
    }

    /**
     * 删除图书信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void deleteBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String id = request.getParameter("id");
        //调用service处理业务逻辑，实现删除功能
        bookService.deleteBook(Integer.parseInt(id));
        //通过重定向跳转到列表功能
        response.sendRedirect(request.getContextPath() + "/BookManagerServlet?method=getPage");
    }

    /**
     * 实现添加和修改图书信息的功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void editBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数
        String id = request.getParameter("id");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String price = request.getParameter("price");
        String sales = request.getParameter("sales");
        String stock = request.getParameter("stock");
        //通过判断id是否为null，执行不同的操作，即修改或添加
        if(id == null){
            //添加
            //将图书信息封装到book对象中
            Book book = new Book(null, title, author, Double.parseDouble(price), Integer.parseInt(sales), Integer.parseInt(stock));
            //调用service处理业务逻辑，实现功能
            bookService.addBook(book);
        }else{
            //修改
            //将图书信息封装到book对象中
            Book book = new Book(Integer.parseInt(id), title, author, Double.parseDouble(price), Integer.parseInt(sales), Integer.parseInt(stock));
            //调用service处理业务逻辑，实现修改功能
            bookService.updateBook(book);
        }
        //通过重定向跳转到列表功能
        response.sendRedirect(request.getContextPath() + "/BookManagerServlet?method=getPage");
    }

    /**
     * 将字符串转换为Integer或Double的方法
     */
    public void string2IntegerOrDouble(){
        //将String转换为Integer
        int i = Integer.parseInt("123");
        Integer ii = new Integer("123");
        Integer iii = Integer.valueOf("123");
        //将String转换为Double
        double d = Double.parseDouble("1.23");
        Double dd = new Double("1.23");
        Double ddd = Double.valueOf("1.23");
    }

}
